package com.demo.day509;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Auther: 86176
 * @Date: 2021/5/19 - 05 - 19 - 22:07
 * @version: 1.0
 */
public class Action {
    Student[] student = new Student[]{
            new Student("张三", "2020666001", "202001", 18),
            new Student("李四", "2020666002", "202002", 19),
            new Student("王五", "2020666003", "202005", 19),
            new Student("七七", "2020666004", "202019", 123),
            new Student("琴", "2020666005", "202012", 22)
    };

    int index = -1;

    public void ShowStudent() {
        for (Student value : student) {
            System.out.println(value.toString());
        }

    }

    public void ShowStudent(String StudentNum) {
        checkId(student,StudentNum);
        System.out.println(student[index].toString());
    }

    public void ChangeInformation(String StudentNum, Scanner scanner) {
        checkId(student,StudentNum);
        if (index < 0 || index > student.length) {
            System.out.println("学号输入有误，请重新尝试！");
        } else {
            System.out.println("请选择需要改的内容：");
            System.out.println("1、姓名 2、年龄 3、班级");
            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    System.out.println("请输入修改后的内容：");
                    String afterName = scanner.next();
                    student[index].setName(afterName);
                    break;
                case 2:
                    System.out.println("请输入修改后的内容：");
                    int afterAge = scanner.nextInt();
                    student[index].setAge(afterAge);
                    break;
                case 3:
                    System.out.println("请输入修改后的内容：");
                    String afterClassNum = scanner.next();
                    student[index].setClassNum(afterClassNum);
                    break;
            }
            System.out.println("修改成功！");
            System.out.println("修改的学生信息变为：");
            ShowStudent(StudentNum);
        }
    }

    public void AddInformation(Scanner scanner) {
        System.out.println("输入新学生姓名");
        String name = scanner.next();


        System.out.println("输入新学生学号（必须为" + (student.length + 1 + 2020666000) + "）");
        String studentNum = scanner.next();
        String a = String.valueOf(student.length + 1 + 2020666000);
        if (studentNum.equals(a)) {

            System.out.println("输入新学生年龄");
            int age = scanner.nextInt();


            System.out.println("输入新学生班级");
            String classNum = scanner.next();

            Student ANewStudent = new Student(name, studentNum, classNum, age);
            Student[] NewStudent = Arrays.copyOf(student, student.length + 1);
            NewStudent[NewStudent.length - 1] = ANewStudent;


            System.out.println("添加成功");
            System.out.println("添加的学生信息为");
            System.out.println(NewStudent[NewStudent.length - 1].toString());

            student = NewStudent;
        }else {
            System.out.println("学号输入错误！即将返回菜单界面！");
        }
    }

    public void ReduceInformation(String StudentNum) {
       checkId(student,StudentNum);
        reduceLength(student,index);
    }

    public void exit() {
        System.exit(0);
    }

    public void reduceLength(Student[] T,int i){
        Student[] NewStudent = new Student[T.length-1];
        if (i==0){
            for (int j = 0; j < NewStudent.length; j++) {
                NewStudent[j] = T[j+1];
            }
            student = NewStudent;
        }
        else if (i==T.length-1){
            for (int j = 0; j < NewStudent.length; j++) {
                NewStudent[j] = T[j];
            }
            student = NewStudent;

        }
        else{
            for (int j = 0; j < i; j++) {
                NewStudent[j] = T[j];
            }
            for (int j = i; j <NewStudent.length ; j++) {
                NewStudent[j] = T[j+1];
            }
            student = NewStudent;
        }
    }

    public int checkId(Student[] T,String studentNum){
        for (int i = 0; i < T.length; i++) {
            if (student[i].getStudentNum().equals(studentNum)){
                index = i;
            }
        }
        return index;
    }
}